import { Transform, Type } from "class-transformer";
import { IsOptional, IsString, IsInt, ValidateNested, MinLength, IsArray } from "class-validator";
import { PartialType } from "@nestjs/mapped-types";
import { BaseQueryDto } from "../base.dto";
import { ApiProperty, OmitType } from "@nestjs/swagger";

export class RoleAddDto {
    @ApiProperty({ description: "角色名, 唯一" })
    @MinLength(2, { message: "角色名称长度不能小于2" })
    @IsString()
    name: string;

    @ApiProperty({ description: "角色描述" })
    @IsString()
    description: string;

    @ApiProperty({ description: "权限列表" })
    @IsArray()
    @IsOptional()
    permissions?: number[];
}

class RoleWhereDto {
    @ApiProperty({ description: "角色名" })
    @IsString()
    @IsOptional()
    name?: string;
}

export class RoleQueryDto extends BaseQueryDto {
    @ApiProperty({ type: RoleWhereDto, description: "查找规则" })
    @ValidateNested()
    @Type(() => RoleWhereDto)
    @IsOptional()
    where?: RoleWhereDto;
}

export class RoleRemoveDto {
    @ApiProperty({ description: "数据库id" })
    @IsInt()
    @Transform(({ value }) => parseInt(value))
    id: number;
}

export class RoleUpdateDto extends PartialType(OmitType(RoleAddDto, ["name"])) {
    @ApiProperty({ description: "数据库id" })
    @IsInt()
    @Transform(({ value }) => parseInt(value))
    id: number;
}
